package com.aizhixin.lab.jsrun.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.service.UserService;
import com.aizhixin.lab.jsrun.domian.CodeDomain;
import com.aizhixin.lab.jsrun.domian.LoginDomain;
import com.aizhixin.lab.jsrun.service.JsRunService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/api/v1/jsrun")
@Api(value = "JSrunapi", tags = "js代码提交运行")
public class JsRunController {

    @Autowired
    private JsRunService jsRunService;
    @Autowired
    private AccountService accountService;


    /**
     * jsrun运行对接接口
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取前端代码", httpMethod = "POST", notes = "接收前端提交代码")
    public ResponseEntity<?> getCode(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "参数为语言类型和代码", required = false) @RequestBody CodeDomain domain) {

        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        return jsRunService.jsRun(domain);
    }


}
